package com.dark.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JSON工具类，jackson实现
 *
 * @author chengluchao
 * @date 2021/1/29
 * @since 1.1.4
 */
public class JsonUtils {

    private static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }

    private JsonUtils() {
    }
    public static Map<String, Object> jsonStrToMapExposeAnomaly(String jsonStr) throws JsonProcessingException {
        try {
            return mapper.readValue(jsonStr, Map.class);
        } catch (JsonProcessingException e) {
            throw e;
        }
    }
    /**
     * json 映射成 bean
     *
     * @param jsonStr
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T jsonStrToObject(String jsonStr, Class<T> valueType) {
        try {
            return mapper.readValue(jsonStr, valueType);
        } catch (IOException e) {
            throw new RuntimeException("json转换异常", e);
        }
    }

    /**
     * 复制对象转换
     * @param jsonStr
     * @param typeReference
     * @param <T>
     * @return
     */
    public static <T> T jsonStrToObject(String jsonStr, TypeReference<T> typeReference) {
        try {
            return mapper.readValue(jsonStr, typeReference);
        } catch (IOException e) {
            throw new RuntimeException("json转换异常", e);
        }
    }

    public static <T> List<T> jsonStrToList(String jsonStr, Class<T> valueType) {
        try {
            JavaType javaType = mapper.getTypeFactory().constructCollectionType(List.class, valueType);
            return mapper.readValue(jsonStr, javaType);
        } catch (IOException e) {
            throw new RuntimeException("json转换异常", e);
        }
    }

    /**
     * json 映射成 bean
     *
     * @param objNode
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T ObjectNodeToObject(ObjectNode objNode, Class<T> valueType) {
        try {
            return mapper.treeToValue(objNode, valueType);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("json转换异常", e);
        }
    }

    public static Map<String, Object> jsonStrToMap(String jsonStr) {
        try {
            return mapper.readValue(jsonStr, Map.class);
        } catch (JsonProcessingException e) {
            return new HashMap<>(2);
        }
    }

    public static <T> Map beanToMap(T t) {
        return jsonStrToMap(toJSONString(t));
    }

    /**
     * bean转json
     *
     * @param input
     * @param <T>
     * @return
     */
    public static <T> String beanToString(T input) {
        try {
            return mapper.writeValueAsString(input);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("json转换异常", e);
        }
    }

    /**
     * bean转json
     *
     * @param input
     * @param <T>
     * @return
     */
    public static <T> String toJSONString(T input) {
        return beanToString(input);
    }

    public static ObjectNode createObjectNode() {
        return mapper.createObjectNode();
    }

    public static ArrayNode createArrayNode() {
        return mapper.createArrayNode();
    }


}
